package com.linkage.common.base;

import com.github.pagehelper.PageInfo;
import com.linkage.common.constant.CommonSysConstant;
import com.linkage.common.util.JSONCustomUtil;
import lombok.Data;

/* *
 * @describe:  类型描述  对外接口返回对象采用标准的JSON格式
 * 由基础数据（base ）和业务数据（buss）两部分组成。
 * @author shaofeng.cheng
 * @title
 * @date 2020/4/19 17:54
 * @param
 * @return
 * @version: V1.0
 */
@Data
public class ResponseInfo<T> {

    /**
     * @Fields base : 基础数据（base ）
     */
    private RespBaseBO base = new RespBaseBO();

    /**
     * @Fields buss : 业务数据（buss）
     */
    private T buss ;

    public ResponseInfo(){}
    public ResponseInfo(RespBaseBO base,T buss){
        this.base = base;
        this.buss = buss;
    }

    public static <T> ResponseInfo error(String msg,T t){
        return returnParam(CommonSysConstant.CODE_ONE,msg,t);
    }

    public static ResponseInfo error(String msg){
        return returnParam(CommonSysConstant.CODE_ONE,msg,null);
    }
    public static ResponseInfo tokenError(String msg){
        return returnParam(CommonSysConstant.CODE_NINE,msg,null);
    }

    public static ResponseInfo successWithPage(PageInfo info){
        RespBaseBO baseVO = new RespBaseBO();
        baseVO.setSuccessCode(CommonSysConstant.CODE_ZERO);
        baseVO.setSuccessMsg(CommonSysConstant.SUCCESS_MSG);
        baseVO.setPages(info.getPages());
        baseVO.setPageSize(info.getPageSize());
        baseVO.setPagePos(info.getPageNum());
        baseVO.setTotal(info.getTotal());
        return new ResponseInfo(baseVO,info.getList());
    }

    public static ResponseInfo success(){
        return returnParam(CommonSysConstant.CODE_ZERO, CommonSysConstant.SUCCESS_MSG,null);
    }
    public static ResponseInfo success(String msg){
        return returnParam(CommonSysConstant.CODE_ZERO, msg,null);
    }
    public static ResponseInfo success(String code,String msg){
        return returnParam(code, msg,null);
    }
    public static <T> ResponseInfo success(T t){
        return returnParam(CommonSysConstant.CODE_ZERO, CommonSysConstant.SUCCESS_MSG,t);
    }

    public static <T> ResponseInfo returnParam(String code,String msg,T t){
        RespBaseBO baseVO = new RespBaseBO();
        baseVO.setSuccessCode(code);
        baseVO.setSuccessMsg(msg);
        return new ResponseInfo(baseVO,t);
    }

    @Override
    public String toString() {
        return JSONCustomUtil.toJsonStr(this);
    }

}
